package com.edu.app.util;


import com.edu.app.constant.ErrorConstant;
import org.springframework.ui.ModelMap;

import java.lang.reflect.Field;
import java.util.Map;

public class ErrorUtil {
    public static ModelMap retErrMsg(String errName){
        ModelMap m = new ModelMap();
        try{
            Class err = Class.forName("com.edu.app.constant.ErrorConstant");
            Field ec = err.getField(errName+"_CODE");
            Field em = err.getField(errName+"_MESSAGE");

            m.put("error_code", ec.get(err));
            m.put("error_message", em.get(err));

            return m;
        }catch (Exception e)
        {
            m.put("error_code", ErrorConstant.SERVER_INTERNAL_ERROR);
            m.put("error", ErrorConstant.SERVER_INTERNAL_ERROR);
        }
        return m;
    }

    public static ModelMap retErrMsg(Exception exception){
        ModelMap m = new ModelMap();
        try{
            Class err = Class.forName("com.edu.app.constant.ErrorConstant");
            Field ec = err.getField("COMMON_EXCEPTION_CODE");

            m.put("error_code", ec.get(err));
            m.put("error_message", exception.getMessage());

            return m;
        }catch (Exception e)
        {
            m.put("error_code", ErrorConstant.SERVER_INTERNAL_ERROR);
            m.put("error", ErrorConstant.SERVER_INTERNAL_ERROR);
        }
        return m;
    }

    public static Map<String, Object> retErrMsgMap(Exception exception){
        ModelMap m = new ModelMap();
        try{
            Class err = Class.forName("com.edu.app.constant.ErrorConstant");
            Field ec = err.getField("COMMON_EXCEPTION_CODE");

            m.put("error_code", ec.get(err));
            m.put("error_message", exception.getMessage());

            return m;
        }catch (Exception e)
        {
            m.put("error_code", ErrorConstant.SERVER_INTERNAL_ERROR);
            m.put("error", ErrorConstant.SERVER_INTERNAL_ERROR);
        }
        return m;
    }
}
